<?PHP
/*

OpenTrack API PHP Framework

This software is written to expedite clean and correct API calls to the DealerTrack Webservices
collectively known as "OpenTrack".  This framework is a simple introduction to how the API
works.  There are areas of incompleteness and oppourtunities for improvement.  If there are
features you need assistance with please contact jarrett@crownmotors.pro

Open Source Initiative OSI - The MIT License:Licensing

The MIT License

We have no copyright on this code. Infact this code is not worthy
of a copyright. However if it helps you in anyway
please send thanks to Jarrett Kuljis @ jarrett@crownmotors.pro

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/
class Parts extends ArkonaDMS_Interop
{
	
	public function PartsInventory($mfg='', $partno='') {
		
		$sp = '<PartsInventory>';
		$sp .= parent::addDealerCode();
			$sp .= '<InventoryParms>';
			// If we have parameters
			if($mfg != '') {
				$sp .= '<Manufacturer>'.$mfg.'</Manufacturer> ';
				//Check for specific Parts
				if($partno != '') {
					$sp .= '<PartNumber>'.$partno.'</PartNumber>';
				}
			}
			
			if($mfg == '' and $partno != '') {
				$sp .= '<PartNumber>'.$partno.'</PartNumber>';
			}
			
			$sp .= '</InventoryParms>';
		$sp .= '</PartsInventory>';
		
		parent::aLog("Requesting Parts Inventory");
		return parent::execRequest($sp);
	}
	
	public function PartsOrderHistory($params) {
		
		if(!is_array($params)) { return false; }
	
		$sp  = '<PartsOrderHistory>';
		$sp .= parent::addDealerCode();
		$sp .= '<OrderHistoryParms>';
		if($params['DaysBack'] !='') {
			$sp .= '<DaysBack>'.$params['DaysBack'].'</DaysBack>';
		} elseif($params['DateStart'] != '' and $params['DateEnd'] != '') {
			$sp .= '<DateStart>'.date("Ymd", strtotime($params['DateStart'])).'</DateStart>';
			$sp .= '<DateEnd>'.date("Ymd", strtotime($params['DateEnd'])).'</DateEnd>';
		} else {
			parent::eLog("Either DaysBack or DateStart and DayEnd required parameters ");
			return false;
		}
		if($params['Manufacturer'] != '' ) { $sp .= '<Manufacturer>'.$params['Manufacturer'].'</Manufacturer> '; }
		if($params['StockingGroup'] != '') { $sp .= '<StockingGroup>'.$params['StockingGroup'].'</StockingGroup>'; }
		$sp .= '</OrderHistoryParms>';
		$sp .= '</PartsOrderHistory>';
		
		parent::aLog("Requesting PartsOrderHistory: ");
		return parent::execRequest($sp);		
	}
	
	public function PartsSalesHistory($params) {
	
		if(!is_array($params)) { parent::eLog("PartsSalesHistory expects array as primary argument"); return false; }
		
		$sp = '<PartsSalesHistory>';
		$sp .= parent::addDealerCode();
		$sp .= '<SalesHistoryParms>';
		if($params['DaysBack'] !='' or $params['InvoiceNumber'] !='') {
			if($params['InvoiceNumber'] != '') { $sp .= '<InvoiceNumber>'.$params['InvoiceNumber'].'</InvoiceNumber>';  }
			if($params['DaysBack'] != '') { $sp .= '<DaysBack>'.$params['DaysBack'].'</DaysBack>'; }
		} elseif($params['DateStart'] != '' and $params['DateEnd'] != '') {
			$sp .= '<DateStart>'.date("Ymd", strtotime($params['DateStart'])).'</DateStart>';
			$sp .= '<DateEnd>'.date("Ymd", strtotime($params['DateEnd'])).'</DateEnd>';
		} else {
			parent::eLog("Either DaysBack or DateStart and DayEnd required parameters ");
			return false;
		}
		if($params['Manufacturer'] != '' ) { $sp .= '<Manufacturer>'.$params['Manufacturer'].'</Manufacturer> '; }
		if($params['SaleType'] != '') { $sp .= '<SaleType>'.$params['SaleType'].'</SaleType>'; }
			
		$sp .= '</SalesHistoryParms>';		
		$sp .= '</PartsSalesHistory>';
		parent::aLog("Requesting PartsSalesHistory: ");
		return parent::execRequest($sp);
	}
	
	// Table Requests
	public function PartsCounterPersonTable() {
		$sp = '<PartsCounterPersonsTableRequest>';
		$sp .= parent::addDealerCode();
		$sp .= '</PartsCounterPersonsTableRequest>';
		parent::aLog("Requesting PartsCounterPersonsTable(): ");
		return parent::execRequest($sp);		
	}
	
	public function PartsManufacturersTable() {
		$sp = '<PartsManufacturersTableRequest>';
		$sp .= parent::addDealerCode();
		$sp .= '</PartsManufacturersTableRequest>';
		parent::aLog("Requesting PartsManufacturersTableRequest(): ");
		return parent::execRequest($sp);		
	}	
	
	public function PartsPromoCodesTable() {
		$sp = '<PartsPromoCodesTableRequest>';
		$sp .= parent::addDealerCode();
		$sp .= '</PartsPromoCodesTableRequest>';
		parent::aLog("Requesting PartsPromoCodesTable(): ");
		return parent::execRequest($sp);		
	}
	
	public function PartsSpecialOrderPrioritiesTable() {
		$sp = '<PartsSpecialOrderPrioritiesTableRequest>';
		$sp .= parent::addDealerCode();
		$sp .= '</PartsSpecialOrderPrioritiesTableRequest>';
		parent::aLog("Requesting PartsSpecialOrderPrioritiesTable(): ");
		return parent::execRequest($sp);		
	}	
	
	public function PartsStockingGroupsTable() {
		$sp = '<PartsStockingGroupsTableRequest>';
		$sp .= parent::addDealerCode();
		$sp .= '</PartsStockingGroupsTableRequest>';
		parent::aLog("Requesting PartsStockingGroupsTable(): ");
		return parent::execRequest($sp);		
	}
					
}